const reg = (() => {
    $.validator.addMethod("checkName", function (val, ele) {
        return /^[a-z]+\w+/i.test(val);
    })
    $.validator.addMethod("checkPhone", function (val, ele) {
        return /^[1][3-9]+\d{9}$/.test(val);
    })
    $.validator.addMethod("checkYzm", function (val, ele) {
        let fa = false;
        let val1 = val.toLowerCase();
        let num = $('#canvas').attr('data-code');
        if (val1 == num) {
            fa = true;
        } else {
            fa = false;
        }
        return fa;
    })


    axios.defaults.baseURL = "";

    axios.interceptors.request.use(function (config) {
        return config;
    })
    axios.interceptors.response.use(function (config) {
        return config;
    })

    const validate = () => {
        $("form").validate({
            rules: {
                uname: {
                    required: true,
                    rangelength: [6, 18],
                    checkName: true
                },
                upwd: {
                    required: true
                },
                pwd: {
                    required: true,
                    equalTo: "#upwd"
                },
                uphone: {
                    checkPhone: true
                },
                yzm: {
                    required: true,
                    checkYzm: true
                },
            },
            messages: {
                uname: {
                    required: "用户必填",
                    rangelength: "用户名长度必须是{0}-{1}",
                    checkName: "用户名必须字母开头"
                },
                upwd: {
                    required: "密码必填"
                },
                pwd: {
                    required: "确认密码必填",
                    equalTo: "2次密码不一致"
                },
                uphone: {
                    checkPhone: "手机号码不合法"
                },
                yzm: {
                    required: "验证码必填",
                    checkYzm: "验证码错误"
                },

            },
            submitHandler() {
                let data = new URLSearchParams();
                data.append("name", $('[name="uname"]').val());
                data.append("pwd", $('[name="upwd"]').val());
                data.append("phone", $('[name="uphone"]').val());
                axios({
                    url: "/account/reg",
                    method: "post",
                    data: data
                }).then(function (res) {
                  
                        layer.msg(res.data.msg)
                        localStorage.setItem("token", res.data.token);
                        localStorage.setItem("user", JSON.stringify(res.data.data));
                  
                })
                return false;
            }
        })
    }
    return {
        validate
    }
})()

$(function () {
    reg.validate();
})